<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header('修改分类')"></head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-cat-edit"
			th:object="${cmsCat}">

			<div class="form-group">
				<label class="col-sm-3 control-label ">上级类目：</label>
				<div class="col-sm-8">
					<div class="input-group">
						<input class="form-control" type="text" onclick="selectCatTree()"
							id="treeName" readonly th:value="${parentName}"> <span
							class="input-group-addon"><i class="fa fa-search"></i></span>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">主目录Id：</label>
				<div class="col-sm-8">
					<input class="form-control disabled" type="text"  id="treeId"name="parentId" th:value="*{parentId}"
						   readonly>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="catName"
						th:field="*{catName}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目类型：</label>
				<div class="col-sm-8">
					<label class="radio-box"> <input type="radio" name="parent"
						value="1" th:field="*{parent}" /> 目录
					</label> <label class="radio-box"> <input type="radio"
						name="parent" value="0" th:field="*{parent}" /> 类目
					</label>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目ID：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="catId"
						th:field="*{catId}" readonly>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目路径：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="catUrl"
						th:field="*{catUrl}" id="catUrl">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目排序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="orderNo"
						th:field="*{orderNo}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目备注：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="remark"
						th:field="*{remark}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目图标：</label>
				<div class="col-sm-8">
					<input id="icon" name="catIcon" class="form-control"
						th:field="*{catIcon}" type="text" placeholder="选择图标">
					<div class="ms-parent" style="width: 100%;">
						<div class="icon-drop animated flipInX"
							style="display: none; max-height: 200px; overflow-y: auto">
							<div data-th-include="system/menu/icon"></div>
						</div>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目状态：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('cms_cat_visible')}">
						<input type="radio" th:id="${dict.dictCode}" name="visible" th:value="${dict.dictValue}"  th:field="*{visible}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
		</form>
	</div>
	<div th:include="include::footer"></div>
	<script>
        var prefix = ctx + "cms/cat";       
        $("#form-cat-edit").validate({
        	rules:{
        		parent:{
        			required:true
        		},
                orderNo:{
        			digits:true
        		},
        		catUrl:{
        			remote: {
                        url: prefix + "/checkCatUrlUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                        	"catUrl" : function() {
                                return $.common.trim($("input[name=catUrl]").val());
                            },
                            "catId" : function() {
                                return $.common.trim($("input[name=catId]").val());
                            }
                        },
                        dataFilter: function(data, type) {
                        	return $.validate.unique(data);
                        }
                    }
        		},
        		catName:{
        			remote: {
                        url: prefix + "/checkCatNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                        	"parentId": function() {
		                		return $("input[name='parentId']").val();
		                    },
                        	"catName" : function() {
                                return $.common.trim($("#catName").val());
                            },
                            "catId" :$("input[name='catId']").val()
                        },
                        dataFilter: function(data, type) {
                        	return $.validate.unique(data);
                        }
                    }
        		},
        	},
        	messages: {
                "catName": {
                    remote: "类目已经存在"
                },
        	 "catUrl": {
                 remote: "路径已经存在"
             }
            },
            focusCleanup: true
        });
        
        function submitHandler() {
	        if ($.validate.form()) {
	            $.operate.save(prefix + "/edit", $('#form-cat-edit').serialize());
	        }
	    }

        $(function() {
        	$("input[name='catIcon']").focus(function() {
                $(".icon-drop").show();
            });
        	$("#form-cat-edit").click(function(event) {
        	    var obj = event.srcElement || event.target;
        	    if (!$(obj).is("input[name='catIcon']")) {
        	    	$(".icon-drop").hide();
        	    }
        	});
        	$(".icon-drop").find(".ico-list i").on("click", function() {
        		$('#icon').val($(this).attr('class'));
            });


			$("input[name='parent']").on('ifChecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定

				if($("input[name='parent']:checked").val()==="0")
				{
					$.ajax({
						//请求方式
						type : "get",
						//请求地址
						url : prefix+"/countCatByParentId/"+$("input[name='catId']").val(),
						//请求成功
						success : function(result) {
							if(result===0)
							{

							}
							else{
								$.modal.msgError("存在子节点不允许修改");
								$("input[name='parent'][value='1']").iCheck('check');
							}
						},
						//请求失败，包含具体的错误信息
						error : function(e){
							console.log(e.status);
							console.log(e.responseText);
						}
					});
				}
			});

        function ggf(res) {

			}
         
        });

        /*菜单类目-x-选择菜单树*/
        function selectCatTree() {
        	var treeId = $("#treeId").val();
        	var menuId = treeId > 0 ? treeId : 1;
        	var url = prefix + "/selectCatTree/1/" + menuId;
			var options = {
				title: '类目选择',
				width: "380",
				url: url,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var body = layer.getChildFrame('body', index),treeId=body.find('#treeId').val();
			if( treeId===$("input[name='catId']").val()){
				layer.confirm('不能选择自己为父节点', {
					btn: ['重新选择'] //按钮
				}, function(c){
					layer.close(c);
				});
		}
			else{
				$("#treeId").val(treeId);
				$("#treeName").val(body.find('#treeName').val());
				layer.close(index);
			}

		}
    </script>
</body>
</html>
